package jin.si.com.web;

import jin.si.com.utils.JsonData;
import jin.si.com.utils.TestAsyncUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @description: 测试异步加载
 * @ClassName TestAsyncController
 * @Author MT
 * @Date 2021/5/25 15:21
 **/

@RestController
@RequestMapping("/async")
public class TestAsyncController {

    @Autowired
    private TestAsyncUtils testAsyncUtils;

    @RequestMapping("/getAsync")
    public JsonData getAsync(){
        long begin = System.currentTimeMillis();
        testAsyncUtils.getTask1();
        testAsyncUtils.getTask2();
        testAsyncUtils.getTask3();
        long end = System.currentTimeMillis();

        return JsonData.buildSuccess(end-begin);
    }


    @RequestMapping("/getAsync1")
    public JsonData getAsync1(){
        long begin = System.currentTimeMillis();
        Future<String> task4 = testAsyncUtils.getTask4();
        Future<String> task5 = testAsyncUtils.getTask5();
        Future<String> task6 = testAsyncUtils.getTask6();
        for(;;){
            if(task4.isDone()&&task5.isDone()&&task6.isDone()){ //.isDone判断方法是否完成

                try {
                    String str1 = task4.get();
                    System.out.println(str1+"****************************");
                    String str2 = task5.get();
                    System.out.println(str2+"****************************");
                    String str3 = task6.get();
                    System.out.println(str3+"****************************");

                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }finally {
                    break;
                }

            }
        }
        long end = System.currentTimeMillis();
        return JsonData.buildSuccess(end-begin);
    }





}
